//
//  HMWelcomeViewController.swift
//  iOSWeibo12
//
//  Created by Admin on 16/9/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMWelcomeViewController: UIViewController {

    /// 头像的底部约束
    var iconViewBottoCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        // 显示沙盒保存的用户的头像
        if let avatar_large = HMUserAccountViewModel.shared.userAccount?.avatar_large {
            let url = URL(string: avatar_large)
            iconView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        // 获取最新用户数据
        HMUserAccountViewModel.shared.loadUserInfo { (error: Error?) in
            // 显示沙盒保存的用户的头像
            if let avatar_large = HMUserAccountViewModel.shared.userAccount?.avatar_large {
                let url = URL(string: avatar_large)
                self.iconView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
            }
        }
    }
    
    // MARK: - 准备UI
    /// 准备UI
    private func setupUI() {
        // 1.添加子控件
        view.addSubview(backgroundImageView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        // 2.添加约束
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景
        // leading
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0))
        // top
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0))
        // trailing
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0))
        // bottom
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0))
        
        // 头像
        // width
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 85))
        // height
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 85))
        // centerX
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        // bottom
        iconViewBottoCons = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: -160)
        self.view.addConstraint(iconViewBottoCons!)
        
        // 欢迎label
        // centerX
        self.view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.iconView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        // top
        self.view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.iconView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 16))
    }
    
    // MARK: - 动画
    // 动画在界面显示出来,我们看到界面的时候再做动画
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        moveAnimation()
    }
    
    /// 头像从下方移动到上方
    private func moveAnimation() {
        // 动画, 使用弹簧效果
        // usingSpringWithDamping: 范围 0 - 1之间, 数值越小弹簧效果月明显
        // initialSpringVelocity: 初始速度.
        UIView.animate(withDuration: 1.2, delay: 0.1, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 更新约束
            self.iconViewBottoCons?.constant = -(UIScreen.main.bounds.height - 160)
            
            // AutoLayout动画必须调用layoutIfNeeded,layoutIfNeeded会根据约束重新设置frame
            self.view.layoutIfNeeded()
        }) { (_) -> Void in
            // 移动动画完成, 是label.alpha = 1
            UIView.animate(withDuration: 1, animations: { () -> Void in
                self.messageLabel.alpha = 1
                }, completion: { (_) -> Void in
                    // TODO: 回到首页
                    print("动画完成")
                    
//                    let appDelegate = UIApplication.shared.delegate as! AppDelegate
//                    appDelegate.switchRootViewController(controller: HMMainTabbarController())
                    AppDelegate.outSwitchRootViewController(controller: HMMainTabbarController())
            })
        }
    }

    // MARK: - 懒加载控件
    /// 背景图片
    private lazy var backgroundImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        
        return imageView
    }()
    
    /// 用户头像
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    /// 用户名称
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.text = "欢迎归来"
        
        // 根据内容自适应
        label.sizeToFit()
        
        return label
    }()
}
